package TUILayer;
import java.util.*;
import ControlLayer.*;


/**
 * Write a description of class RentUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RentUI
{
    private AddressBookUI addrBUI;
    private DvdUI dvdUI;
    private RentCtr reCtr;
    private AddressBookUI addrUI;

    /**
     * Constructor for objects of class RentUI
     */
    public RentUI()
    {
    }
    public void start(DvdUI dui, AddressBookUI aui)
    {
        dvdUI = dui;
        addrBUI = aui;
        reCtr = new RentCtr();
        rentMenu();
        
    }
    
    public void rentMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeRentMenu();
            if (choise == 1)
            {
                rentDVD();
            }
            else
            {
                if(choise == 2)
                {
                   deliverDVD();
                }
                    else
                    {
                        exit = true;
                }//end else
            }//end else
        }//end while
    }
    
    private int writeRentMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Rent Menu ***");
            System.out.println(" (1) Rent DVD");
            System.out.println(" (2) Deliver DVD");
            System.out.println(" (3) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    private int inputID()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int id = 0;
        Scanner keyboard = new Scanner(System.in);
        while(!ok)
        {
            System.out.println("Write ID: ");
            try
            {
                id = keyboard.nextInt();
                ok = true;
            }
           catch (Exception e)
            {
             System.out.println("Input must be a number - try again.");
             String input = keyboard.nextLine();
           }
        }
        return id;
    }
    private int inputPersonID()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int id = 0;
        Scanner keyboard = new Scanner(System.in);
        while(!ok)
        {
            System.out.println("Write your ID: ");
            try
            {
                id = keyboard.nextInt();
                ok = true;
            }
           catch (Exception e)
            {
             System.out.println("Input must be a number - try again.");
             String input = keyboard.nextLine();
           }
        }
        return id;
    }
    private String inputDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the date: ");
        String date = keyboard.nextLine();
        return date;
    }
    
    private void rentDVD()
    {
        int id = inputID();
        dvdUI.findDVDforRent(id);
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Do you want to rent this movie? 1 = yes, 2 = no");
        int choice = keyboard.nextInt();
        if(choice == 1)
        {
            try
            {
                
                reCtr.rentDVDSample(dvdUI.getDVDId(id), addrBUI.getPersonID(inputPersonID()), id, inputDate());
                System.out.println("Done");
                pause();
            }
            catch(Exception e)
            {
                System.out.println("There was an error.");
                pause();
            }
        }
    }
    private void deliverDVD()
    {
        reCtr.deliverRent(inputID());
    }
    
    private void pause()
    {
       Scanner keyboard = new Scanner(System.in);
       String wait = keyboard.nextLine();
    }
}
